package tech.heptagram.staff.api.v3;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import tech.heptagram.core.common.rest.ResponseResult;
import tech.heptagram.staff.model.ProcessRecord;
import tech.heptagram.staff.query.ProcessRecordQuery;
import tech.heptagram.staff.query.RejectMsgReq;
import tech.heptagram.staff.service.ProcessRecordService;

/**
 * @author wang.yuchuan
 * @createdAt 2019/3/22
 */
@Api("审批流相关api")
@RestController
@RequestMapping("/api/v3/bpms-record")
@Slf4j
public class ProcessRecordApiRestController implements ProcessRecordApi {

    @Resource
    private ProcessRecordService processRecordService;

    @ApiOperation("保存审批单")
    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody ProcessRecord vo) {
        processRecordService.save(vo);
        return new ResponseResult();
    }

    @ApiOperation("修改申请审批状态到开始")
    @PostMapping("/update/state/start")
    @Override
    public ResponseResult updateStateToStart(@RequestBody ProcessRecord vo) {
        processRecordService.updateStateToStart(vo.getBpmsProcessId());
        return new ResponseResult();
    }

    @ApiOperation("修改申请审批状态到撤销")
    @PostMapping("/update/state/terminate")
    @Override
    public ResponseResult updateStateToTerminate(@RequestBody ProcessRecord vo) {
        processRecordService.updateStateToTerminate(vo.getBpmsProcessId());
        return new ResponseResult();
    }

    @ApiOperation("修改申请审批状态到结束")
    @PostMapping("/update/state/finish")
    @Override
    public ResponseResult updateStateToFinish(@RequestBody ProcessRecord vo) {
        processRecordService.updateStateToFinish(vo.getBpmsProcessId(),vo.getApplicationResult());
        return new ResponseResult();
    }

    @ApiOperation("修改申请拒绝消息")
    @PostMapping("/updateRejectMsg")
    @Override
    public ResponseResult updateRejectMessage(RejectMsgReq rejectMsg) {
        processRecordService.updateRejectMessage(rejectMsg.getBpmsProcessId(),rejectMsg.getRejectMessage());
        return new ResponseResult();
    }

    @ApiOperation("根据审批ID获取审批记录")
    @PostMapping("/findProcessRecord")
    @Override
    public ResponseResult<ProcessRecord> findByBpmsProcessId(@RequestBody ProcessRecord vo) {
        ProcessRecord record = processRecordService.findByBpmsProcessId(vo.getBpmsProcessId());
        return new ResponseResult<>(record);
    }

    @ApiOperation("根据条件获取审批单纪录")
    @PostMapping("/find")
    @Override
    public ResponseResult<List<ProcessRecord>> findByUserProcessTypeAndState(@RequestBody ProcessRecordQuery processRecord) {
        List<ProcessRecord> lstProcessRecords = processRecordService.findByUserProcessTypeAndState(processRecord.getStaffCode(), processRecord.getProcessType(), processRecord.getProcessState());
        return new ResponseResult<>(lstProcessRecords);
    }
}
